package io.adminboot.service.impl;

import io.adminboot.domain.vo.PageVO;
import io.adminboot.entity.Article;
import io.adminboot.repository.extend.ArticleExtMapper;
import io.adminboot.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class ArticleServiceImpl implements IArticleService {

	@Autowired
	private ArticleExtMapper articleMapper;

	@Override
	public PageVO queryPage(String title, Integer pageNum, Integer pageSize) {
		List<Article> list = articleMapper.selectList(title, (pageNum - 1) * pageSize, pageSize);
		int totalCount = articleMapper.count(title);
		PageVO page = new PageVO(list, totalCount, pageSize, pageNum);
		return page;
	}

	@Override
	public void save(Article article) {
		article.setGmtCreate(new Date());
		articleMapper.insert(article);
	}

	@Override
	public void update(Article article) {
		// 只更新表单字段 updateByPrimaryKeySelective
		article.setGmtModified(new Date());
		articleMapper.updateByPrimaryKeySelective(article);
	}

	@Override
	public void deleteBatch(Long[] ids) {
		articleMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public Article selectById(Integer id) {
		return articleMapper.selectByPrimaryKey(id);
	}

}
